/**
 * @overview 创建面或体
 * @author dengqiang
 * @description 原生Cesium polygon
 * @update  2022.03.21
 */
class GPolygon {
  constructor() {
    window.GPolygonArray = []
  }

  //新增单个体或面
  add(paramObj) {
    const { name, option, parentName } = paramObj
    const hierarchy = this.changeXYArrayToCar3(option.polygon.position)
    const position = Cesium.Cartesian3.fromDegrees(
      option.centerpos[0],
      option.centerpos[1],
      option.centerpos[2]
    )
    const entity = {
      id: option.id || undefined,
      name: option.name || '',
      position: position,
      label: {
        text: option.label.name || '',
        font: option.label.font || '18px san-serif',
        pixelOffset: option.label.labelOffset || new Cesium.Cartesian2(0, -10),
        distanceDisplayCondition:
          option.label.distanceDisplayCondition || new Cesium.DistanceDisplayCondition(0, 6000),
        disableDepthTestDistance: 10000,
        showBackground: true, //是否显示背景
        backgroundColor: option.label.backgroundColor || new Cesium.Color(0.165, 0.165, 0.165, 0.8), //背景颜色
        backgroundPadding: option.label.backgroundPadding || new Cesium.Cartesian2(7, 5), //背景边距
        fillColor: option.label.fillColor || Cesium.Color.WHITE, //填充色
        outlineColor: option.label.outlineColor || new Cesium.Color(0, 1, 1, 0.8), //外线颜色
        outlineWidth: option.label.outlineWidth || 1.0, //外线宽
      },
      polygon: {
        hierarchy: hierarchy,
        // material: option.polygon.color || Cesium.Color.BLUE.withAlpha(0.7),
        // material: "rgba(111,255,2,0.9)",
        material:
          Cesium.Color.fromCssColorString(option.polygon.color) || Cesium.Color.BLUE.withAlpha(0.7),
        perPositionHeight: true,
        outline: true,
        outlineColor: Cesium.Color.BLUE.withAlpha(0.7),
        outlineWidth: 1.0,
        distanceDisplayCondition:
          option.polygon.distanceDisplayCondition || Cesium.DistanceDisplayCondition(10, 1000),
        extrudedHeight: option.polygon.extrudedHeight || 60,
      },
    }
    const polygon = window.map3DControl.viewer.entities.add(entity)
    polygon.attribute = option.attribute //支持添加自定义属性
    const polygonObject = {
      name: option.name,
      parentName: parentName,
      polygon: polygon,
    }
    window.GPolygonArray.push(polygonObject)
  }

  //根据name移除单个面或体
  removeByName(name) {
    window.GPolygonArray.forEach((item, index) => {
      if (item.name == name) {
        window.GPolygonArray.splice(index, 1)
        window.map3DControl.viewer.entities.remove(item.polygon)
      }
    })
  }

  //根据parentName移除一批面或体
  removeByPatName(parentName) {
    const length = window.GPolygonArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GPolygonArray[i].parentName == parentName) {
        window.map3DControl.viewer.entities.remove(window.GPolygonArray[i].polygon)
        window.GPolygonArray.splice(i, 1)
      }
    }
  }

  //移除所有面或体
  removeAll() {
    window.GPolygonArray.forEach((item) => {
      window.map3DControl.viewer.entities.removeAll()
    })
    window.GPolygonArray = []
  }

  //根据name控制单个面或体的显隐
  setVisibleByName(name, boolean) {
    window.GPolygonArray.forEach((item) => {
      if (item.name == name) {
        item.polygon.show = boolean
      }
    })
  }

  //根据parentName控制一批面或体的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GPolygonArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.polygon.show = boolean
      }
    })
  }

  changeXYArrayToCar3(array) {
    const car3Array = []
    array.forEach((ele) => {
      car3Array.push(ele[0])
      car3Array.push(ele[1])
      car3Array.push(ele[2])
    })
    const res = Cesium.Cartesian3.fromDegreesArrayHeights(car3Array)
    return res
  }
}

export default GPolygon
